package com.bro.person.admin.config.security;

import com.bro.person.admin.interceptors.AuthorizeInterceptor;
import com.bro.person.base.enums.error.GlobalEnum;
import com.bro.person.base.utils.ResponseBuilderUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL;

/**
 * @author swallow
 * Created by swallow on 2018/11/10.<br>
 */
@EnableWebMvc
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Bean
    public AuthorizeInterceptor authorizeInterceptor() {
        return new AuthorizeInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authorizeInterceptor())
                .excludePathPatterns("/static/*")
                .excludePathPatterns("/templates/*")
                .excludePathPatterns("/login")
                .excludePathPatterns("/error")
                .excludePathPatterns("/pages/login")
                .addPathPatterns("/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/META-INF/resources/")
                .addResourceLocations("classpath:/resources/")
                .addResourceLocations("classpath:/static/");
    }

    /**
     * 解决跨域问题
     * @param registry  ** 代表所有路径 allowOrigin 指可以通过的ip * 代表所有
     *                  可以使用指定的ip 使用逗号分隔
     *                  默认为* 可替换为"GET", "POST", "HEAD", "PUT", "DELETE"
     *                  最大过期时间 默认-1 表示没有过期时间
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .maxAge(3600)
                .allowedHeaders("*");
    }

//    @Override
//    public void configureViewResolvers(ViewResolverRegistry registry) {
//        registry.enableContentNegotiation(jsonView());
//    }
//
//    private MappingJackson2JsonView jsonView() {
//        ObjectMapper objectMapper = new ObjectMapper();
//        objectMapper.setSerializationInclusion(NON_NULL);
//
//        MappingJackson2JsonView view = new MappingJackson2JsonView();
//
//        view.setPrettyPrint(true);
//        view.setExtractValueFromSingleKeyModel(true);
//        view.setObjectMapper(objectMapper);
//        view.setContentType("application/json;charset=UTF-8");
//        view.setModelKey("jsonResult");
//
//        return view;
//    }
}
